//
//  MSHUDCircleProgressView.m
//  BOPMSProgress
//
//  Created by zz on 6.11.23.
//

#import "MSHUDCircleProgressView.h"


#define  MSHUDWidth [UIScreen mainScreen].bounds.size.width
#define  MSHUDHeight [UIScreen mainScreen].bounds.size.height
#define MSCIRCLETAG 8888

@interface MSHUDCircleProgressView()

@property (nonatomic, strong) UIView *circleContentView;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property (nonatomic, assign) NSInteger duration;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UILabel *countdownLabel;
@property (nonatomic, assign) NSInteger countdownSeconds;

@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) UILabel * subLabel;

@end

@implementation MSHUDCircleProgressView
-(UIColor*)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha {
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha];
}

-(instancetype)initWithDuration:(NSInteger)Duration {
    if (self = [super initWithFrame:CGRectZero]) {
        self.countdownSeconds = Duration;
        self.duration = Duration;
        self.strokeColor = [self colorWithRed:29 green:206 blue:159 alpha:1];
        self.lineWidth = 3;
        self.textColor = [self colorWithRed:29 green:206 blue:159 alpha:1];
        self.font = [UIFont fontWithName:@"PingFangSC-Regular" size:28];
        
        self.titleText = @"In Progress";
        self.titleTextColor = [self colorWithRed:0 green:0 blue:0 alpha:0.85];
        self.titleFont = [UIFont fontWithName:@"SFUIText-Semibold" size:17];
        
        self.subText = @"Face recognition,Please wait patiently";
        self.subTextColor = [self colorWithRed:0 green:0 blue:0 alpha:0.65];
        self.subFont = [UIFont systemFontOfSize:13];
        
        [self setupShapeLayer];
        [self startCountdown];
        [self config_setting];
        self.backgroundColor = [UIColor clearColor];
    }
    return  self;
}

- (CGSize)intrinsicContentSize {
    return CGSizeMake(270, 152);
}

-(void)setupShapeLayer {
    CGFloat W = 270;
    CGFloat H = 152;
    CGFloat X = 0;
    CGFloat Y = 0;
    self.circleContentView = [[UIView alloc] initWithFrame:CGRectMake(X, Y, W, H)];
    self.circleContentView.layer.cornerRadius = 14;
    self.circleContentView.layer.masksToBounds = YES;
    self.circleContentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.circleContentView];
    

    CGPoint center = CGPointMake(W * 0.5, 46);
    CGFloat radius = 26;
    CGFloat startAngle = 3 * M_PI_2;
    CGFloat endAngle = -M_PI_2;
    UIBezierPath *circularPath = [UIBezierPath bezierPathWithArcCenter:center
                                                                radius:radius
                                                            startAngle:startAngle
                                                              endAngle:endAngle
                                                             clockwise:NO];

    self.shapeLayer = [CAShapeLayer layer];
    self.shapeLayer.path = circularPath.CGPath;
    self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
    self.shapeLayer.lineCap = kCALineCapRound;
    self.shapeLayer.strokeStart  = 0;
    self.shapeLayer.strokeEnd = 1;
    [self.circleContentView.layer addSublayer:self.shapeLayer];
    
    // 创建倒计时标签
    self.countdownLabel = [[UILabel alloc] init];
    self.countdownLabel.frame = CGRectMake(0, 0, 100, 30);
    self.countdownLabel.center = center;
    self.countdownLabel.text = [NSString stringWithFormat:@"%ld", (long)_countdownSeconds];
    self.countdownLabel.textAlignment = NSTextAlignmentCenter;
    self.countdownLabel.textColor = self.textColor;
    self.countdownLabel.font = self.font;
    [self.circleContentView addSubview:self.countdownLabel];
    
    self.titleLabel = [[UILabel alloc] init];
    CGFloat tY = self.countdownLabel.center.y + radius + 15;
    self.titleLabel.frame = CGRectMake(0, tY, W, 24);
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.text = self.titleText;
    self.titleLabel.textColor = self.titleTextColor;
    self.titleLabel.font = self.titleFont;
    [self.circleContentView addSubview:self.titleLabel];
    
    self.subLabel = [[UILabel alloc] init];
    CGFloat sY = CGRectGetMaxY(self.titleLabel.frame) + 2;
    self.subLabel.frame = CGRectMake(10, sY, W - 20, 20);
    self.subLabel.textAlignment = NSTextAlignmentCenter;
    self.subLabel.text = self.subText;
    self.subLabel.textColor = self.subTextColor;
    self.subLabel.font = self.subFont;
    [self.circleContentView addSubview:self.subLabel];

}

-(void)config_setting {
    self.shapeLayer.strokeColor = self.strokeColor.CGColor;
    self.shapeLayer.lineWidth = self.lineWidth;
    self.countdownLabel.textColor = self.textColor;
    self.countdownLabel.font = self.font;
    
    self.titleLabel.textColor = self.titleTextColor;
    self.titleLabel.font = self.titleFont;
    
    self.subLabel.textColor = self.subTextColor;
    self.subLabel.font = self.subFont;
}

- (void)startCountdown {
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES];
}



- (void)updateCountdown {
    _countdownSeconds--;
    _countdownLabel.text = [NSString stringWithFormat:@"%ld", (long)_countdownSeconds];
    
    CGFloat progress = (CGFloat)_countdownSeconds / self.duration;
    _shapeLayer.strokeEnd = progress;
    
    if (_countdownSeconds == 0) {
        [_timer invalidate];
        _timer = nil;
    }
}

@end
